﻿using System;
using System.Linq;
using System.Web.Mvc;
using viBook.Models;
using viBook.Repositories;

namespace viBook.Controllers
{
    public class HomeController : BaseController
    {
        #region Constructors and Repository vars

        private readonly IBookRepository _bookRepository;
        private readonly ICategoryRepository _categoryRepository;
        private readonly IContentTagRepository _contentTagRepository;
        private readonly IUserRepository _userRepository;

        public HomeController(
            ICategoryRepository categoryRepository,
            IBookRepository bookRepository,
            IContentTagRepository contentTagRepository,
            IUserRepository userRepository
            )
        {
            _categoryRepository = categoryRepository;
            _bookRepository = bookRepository;
            _contentTagRepository = contentTagRepository;
            _userRepository = userRepository;
        }

        #endregion

        public ActionResult Index()
        {
            ViewBag.ListCategories = _categoryRepository.GetAllCategories();
            ViewBag.ListMostPopularTags = _contentTagRepository.GetMostPopularTags();
            ViewBag.ListMostPopularUser = _userRepository.GetMostPopularUsers();
            ViewBag.ListMostPopularBook = _bookRepository.GetMostPopularBook();
            return View();
        }


        public ActionResult Search()
        {
           
            string searchConten = string.Empty;
            if (!string.IsNullOrEmpty(Request["search-text"]))
            {
                var stringSearchcontent = Request["search-text"].Split(new[] {' '},
                                                                       StringSplitOptions.RemoveEmptyEntries);
                searchConten = stringSearchcontent.Where(content => !string.IsNullOrWhiteSpace(content)).Aggregate(searchConten, (current, content) => current + (content + " "));
            }
            return RedirectToAction("SearchResult", "Books", new { searchContent = searchConten });
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult TermOfUse()
        {
            return View();
        }

        public ActionResult FAQ()
        {
            return View();
        }
    }
}